<template>
  <PanerGrid @onSubmit="handleSubmit" @onReset="handleReset">
    <template v-slot:search>
      <div class="d-group-items d-layout-grid d-layout-grid-4">
        <div class="d-grid-item">
          <div class="d-form-group d-form-group-flex">
            <div class="d-form-label d-text-right">
              <label class="label-text">名称</label>
            </div>
            <div class="d-form-el">
              <input v-model="search.name" class="d-form-input" />
            </div>
          </div>
        </div>
        <div class="d-grid-item">
          <div class="d-form-group d-form-group-flex">
            <div class="d-form-label d-text-right">
              <label class="label-text">类型</label>
            </div>
            <div class="d-form-el">
              <select v-model="search.cat" class="d-form-select">
                <option v-for="item in archiveCatList" :key="item.id" :value="item.id">{{ item.text }}</option>
              </select>
            </div>
          </div>
        </div>
        <div class="d-grid-item">
          <div class="d-form-group d-form-group-flex">
            <div class="d-form-label d-text-right">
              <label class="label-text">分类</label>
            </div>
            <div class="d-form-el">
              <select v-model="search.cid" class="d-form-select">
                <option v-for="item in archiveCidList" :key="item.id" :value="item.id">{{ item.text }}</option>
              </select>
            </div>
          </div>
        </div>
        <div class="d-grid-item">
          <div class="d-form-group d-form-group-flex">
            <div class="d-form-label d-text-right">
              <label class="label-text">标签</label>
            </div>
            <div class="d-form-el">
              <input v-model="search.labels" class="d-form-input" type="text" />
            </div>
          </div>
        </div>
        <div class="d-grid-item">
          <div class="d-form-group d-form-group-flex">
            <div class="d-form-label d-text-right">
              <label class="label-text">内容</label>
            </div>
            <div class="d-form-el">
              <input v-model="search.items" class="d-form-input" />
            </div>
          </div>
        </div>
        <div class="d-grid-item">
          <div class="d-form-group d-form-group-flex">
            <div class="d-form-label d-text-right">
              <label class="label-text">描述</label>
            </div>
            <div class="d-form-el">
              <input v-model="search.description" class="d-form-input" />
            </div>
          </div>
        </div>
        <div class="d-grid-item">
          <FormCalendar
            v-model="search.createTime"
            label="创建时间"
            labelWidth="80px"
            class="d-form-group-flex"
          ></FormCalendar>
        </div>
        <div class="d-grid-item">
          <FormCalendar
            v-model="search.updateTime"
            label="修改时间"
            labelWidth="80px"
            class="d-form-group-flex"
          ></FormCalendar>
        </div>
        <div v-if="false" class="d-grid-item">
          <div class="d-form-group d-form-group-flex">
            <div class="d-form-label d-text-right">
              <label class="label-text">创建人</label>
            </div>
            <div class="d-form-el">
              <input class="d-form-input" />
            </div>
          </div>
        </div>
        <div v-if="false" class="d-grid-item">
          <div class="d-form-group d-form-group-flex">
            <div class="d-form-label d-text-right">
              <label class="label-text">修改人</label>
            </div>
            <div class="d-form-el">
              <input class="d-form-input" />
            </div>
          </div>
        </div>
      </div>
    </template>
    <template v-slot:header>
      <div class="d-paner-caption">
        <span class="d-caption-text">文档列表</span>
        <div class="d-bar-tools d-bar-tools-condensed">
          <span class="d-tools-item"><a class="d-btn d-btn-plain-sign" :href="getMicroPath('#/archive/item')">添加</a></span>
          <span @click="handleDelChecked" class="d-tools-item"><button class="d-btn d-btn-plain-sign" type="button">删除</button></span>
        </div>
      </div>
      <div class="d-bar-tools d-bar-tools-right d-bar-tools-condensed">
        <a @click="handleReload" class="d-tools-item"><span class="d-btn d-btn-plain-sign d-btn-square-sm d-btn-flex"><RefreshIcon /></span></a>
      </div>
    </template>
    <template v-slot:body>
      <Grid v-model="list" ref="gridList" :request="gridRequest" :pageSize="15" @onClickPage="handleClickPage">
        <GridColumn :width="48" type="checkbox" />
        <GridColumn :width="60" type="index" label="序号" />
        <GridColumn field="name" label="名称">
          <template v-slot:body="scope">
            <a :href="getMicroPath('#/archive/view/' + scope.id)" class="d-text-theme d-text-linker">{{ scope.name }}</a>
          </template>
        </GridColumn>
        <GridColumn field="cat" label="类型" format="select" :select="archiveCatList" width="120" />
        <GridColumn field="cid" label="分类" format="select" :select="archiveCidList" width="120" />
        <GridColumn field="status" label="状态" format="select" :select="statusList" width="80" />
        <GridColumn field="updateTime" label="修改时间" format="datetime|yyyy-MM-dd HH:mm:ss" width="160" />
        <!-- <GridColumn field="updateUser" label="修改人" /> -->
        <GridColumn field="createTime" label="创建时间" format="datetime|yyyy-MM-dd HH:mm:ss" width="160" />
        <!-- <GridColumn field="createUser" label="创建人" /> -->
        <GridColumn field="action" :width="120" label="操作">
          <template v-slot:body="scope">
            <div class="d-bar-tools d-bar-tools-condensed d-grid-actions">
              <a :href="getMicroPath('#/archive/item/' + scope.id)" class="d-tools-item d-text-theme"><EditIcon size="sm" /></a>
              <a @click="handleDelItem(scope)" class="d-tools-item d-text-theme"><DeleteIcon size="sm" /></a>
            </div>
          </template>
        </GridColumn>
      </Grid>
    </template>
  </PanerGrid>
</template>

<script>
import { PanerGrid } from '@daelui/vdog/dist/pages'
import { Grid, GridColumn, Dialoger, FormCalendar, Pager } from '@daelui/vdog/dist/components'
import { RefreshIcon, EditIcon, DeleteIcon } from '@daelui/vdog/dist/icons'
import { zh as locale } from '@daelui/dogjs/dist/locale'
import arcSer, { api } from '../../service/action/archive.action.js'
import dicSer from '@/service/action/dictionary.action.js'
import { getter, state } from '../../service/stock/index.js'

export default {
  components: {
    PanerGrid,
    Grid,
    GridColumn,
    RefreshIcon,
    EditIcon,
    DeleteIcon,
    FormCalendar,
    Pager
  },

  data () {
    return {
      // 搜索条件
      search: {
        name: '',
        cat: state.archiveCatList.ARCHIVE,
        cid: '',
        labels: '',
        description: '',
        createTime: '',
        updateTime: ''
      },
      list: [],
      // 表格请求配置
      gridRequest: {
        url: api.queryListPage.url, // 请求地址
        method: api.queryListPage.method, // 请求方式
        params: {
          cat: state.archiveCatList.ARCHIVE
        }
      },
      // 分类
      archiveCatList: state.archiveCatList,
      // 状态
      statusList: getter.statusList(),
      // 文档分类列表
      archiveCidList: []
    }
  },

  methods: {
    /**
     * @function 查询事件处理
    */
    handleSubmit () {
      this.$refs.gridList.reload(this.search)
    },

    /**
     * @function 重置事件
    */
    handleReset () {
      this.search = {
        name: '',
        cat: state.archiveCatList.ARCHIVE,
        cid: '',
        labels: '',
        description: '',
        createTime: '',
        updateTime: ''
      }
    },

    /**
     * @function 删除选定的数据
    */
    handleDelChecked () {
      let items = this.$refs.gridList.getChecked()
      this.removeItems(items)
    },

    /**
     * @function 删除单条数据
    */
    handleDelItem (item) {
      this.removeItems([item])
    },

    /**
     * @function 删除多条数据
    */
    removeItems (items) {
      items = Array.isArray(items) ? items : []
      if (items.length < 1) {
        Dialoger.warn(locale.list.selectOne)
        return false
      }
      Dialoger.confirm(locale.message.confirmDelete).then(b => {
        if (!b) {
          return false
        }
        items = items.map(item => item.id)
        arcSer.removeItem(items).then(result => {
          this.$refs.gridList.reload()
        })
      })
    },

    /**
     * @function 重新加载
    */
    handleReload () {
      this.$refs.gridList.reload(this.search)
    },

    /**
     * @function 分页点击事件
    */
    handleClickPage (data) {
      // data = data || {}
      // this.handleReload()
    }
  },

  beforeMount () {
    // 文档分类
    dicSer.queryAllArcCidList(false).then(list => {
      list.unshift({ id: '', text : '全部'})
      this.archiveCidList = list
    })
  }
}
</script>

<style scoped>
.d-form-label {
  width: 80px;
}
</style>
